<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人资料 - 麻醉知识图谱</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Arial, sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            padding: 0;
            overflow-x: hidden;
        }

        .navbar-custom {
            background-color: #2E3B4E;
            padding: 1rem 2rem;
        }
        .nav-button {
            background-color: #2E3B4E;
            color: white;
            font-size: 1.1rem;
            margin-right: 1rem;
            border: none;
            padding: 8px;
        }

        .nav-button:hover {
            background-color: #1F2A3A;
            border-radius: 5px;
        }
        .navbar-custom .navbar-nav .nav-link {
            color: white;
            font-size: 1.1rem;
            margin-right: 1rem;
        }

        .navbar-custom .navbar-nav .nav-link:hover {
            background-color: #1F2A3A;
            border-radius: 5px;
        }

        .container {
            margin-top: 5rem;
            padding: 2rem;
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
            animation: fadeIn 2s;
        }

        .btn-custom {
            background-color: #2E3B4E;
            color: white;
            transition: background-color 0.3s;
        }

        .btn-custom:hover {
            background-color: #1F2A3A;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
            }

            to {
                opacity: 1;
            }
        }
    </style>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-custom fixed-top">
        <div class="container-fluid">
            <a class="navbar-brand" href="/page">麻醉知识图谱</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="/page">主页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" onclick="checkLoginAndRedirect('/knowledge')">知识图谱</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" onclick="checkLoginAndRedirect('/profile')">个人中心</a>
                    </li>
                    <li class="nav-item">
                    <button id="logoutButton" class="nav-button"  onclick="logout()">注销</button>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container">
        <h1>个人资料</h1>
        <form id="profileForm">
            <div class="mb-3">
                <label for="email" class="form-label">电子邮件</label>
                <input type="email" class="form-control" id="email" placeholder="电子邮件">
            </div>
            <div class="mb-3">
                <label for="full_name" class="form-label">全名</label>
                <input type="text" class="form-control" id="full_name" placeholder="全名">
            </div>
            <div class="mb-3">
                <label for="clinical_title" class="form-label">临床职称</label>
                <input type="text" class="form-control" id="clinical_title" placeholder="临床职称">
            </div>
            <div class="mb-3">
                <label for="teaching_title" class="form-label">教学职称</label>
                <input type="text" class="form-control" id="teaching_title" placeholder="教学职称">
            </div>
            <div class="mb-3">
                <label for="department" class="form-label">所在科室</label>
                <input type="text" class="form-control" id="department" placeholder="所在科室">
            </div>
            <button type="button" class="btn btn-custom" onclick="updateProfile()">更新资料</button>
        </form>
        <hr>
        <h2>更改密码</h2>
        <form id="changePasswordForm">
            <div class="mb-3">
                <label for="current_password" class="form-label">当前密码</label>
                <input type="password" class="form-control" id="current_password" placeholder="当前密码">
            </div>
            <div class="mb-3">
                <label for="new_password" class="form-label">新密码</label>
                <input type="password" class="form-control" id="new_password" placeholder="新密码">
            </div>
            <div class="mb-3">
                <label for="confirm_new_password" class="form-label">确认新密码</label>
                <input type="password" class="form-control" id="confirm_new_password" placeholder="确认新密码">
            </div>
            <button type="button" class="btn btn-custom" onclick="changePassword()">更改密码</button>
        </form>
    </div>

    <script>
        function checkLoginAndRedirect(destination) {
            fetch('/check_login_status')
                .then(response => response.json())
                .then(data => {
                    if (!data.logged_in) {
                        alert("请先登录！");
                    } else {
                        if (destination == '/knowledge') {
                            if (data.userRole == 'admin') {
                                window.location.href = '/knowledge_admin';
                            } else {
                                window.location.href = '/knowledge';
                            }
                        } else {
                            window.location.href = destination;
                        }

                    }
                })
                .catch(error => {
                    console.error('检查登录状态时发生错误:', error);
                });
        }

        document.addEventListener('DOMContentLoaded', function () {
            // 页面加载时获取用户信息并填充表单
            fetch('/get_profile', {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                }
            }).then(response => response.json())
                .then(data => {
                    if (data.success) {
                        document.getElementById('email').value = data.profile.email;
                        document.getElementById('full_name').value = data.profile.full_name;
                        document.getElementById('clinical_title').value = data.profile.clinical_title;
                        document.getElementById('teaching_title').value = data.profile.teaching_title;
                        document.getElementById('department').value = data.profile.department;
                    } else {
                        alert(data.error);
                    }
                });
        });

        function updateProfile() {
            const email = document.getElementById('email').value;
            const full_name = document.getElementById('full_name').value;
            const clinical_title = document.getElementById('clinical_title').value;
            const teaching_title = document.getElementById('teaching_title').value;
            const department = document.getElementById('department').value;

            fetch('/update_profile', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `email=${encodeURIComponent(email)}&full_name=${encodeURIComponent(full_name)}&clinical_title=${encodeURIComponent(clinical_title)}&teaching_title=${encodeURIComponent(teaching_title)}&department=${encodeURIComponent(department)}`
            }).then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('资料更新成功！');
                    } else {
                        alert(data.error);
                    }
                });
        }

        function changePassword() {
            const current_password = document.getElementById('current_password').value;
            const new_password = document.getElementById('new_password').value;
            const confirm_new_password = document.getElementById('confirm_new_password').value;

            if (new_password !== confirm_new_password) {
                alert('新密码和确认新密码不匹配');
                return;
            }

            fetch('/change_password', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `current_password=${encodeURIComponent(current_password)}&new_password=${encodeURIComponent(new_password)}`
            }).then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('密码更改成功！');
                    } else {
                        alert(data.error);
                    }
                });
        }
        function logout() {
        fetch('/logout')
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    window.location.href = '/';
                } else {
                    alert(data.error);
                }
            });
        }
    </script>
</body>

</html>